import java.util.Stack;

public class demo2 {
    class Solution {
        public boolean isValid(String s) {
            Stack<Character> stack=new Stack<>();
            char[]ss=s.toCharArray();
            int ret=0;
            while (ret<s.length()){
                if(ss[ret]=='{'||ss[ret]=='['||ss[ret]=='('){
                    stack.push(ss[ret]);
                }else{
                    if(stack.isEmpty())return false;
                    if(ss[ret]=='}'){
                        if(stack.peek()=='{'){
                            stack.pop();
                        }else return false;
                    }
                    if(ss[ret]=='['){
                        if(stack.peek()==']'){
                            stack.pop();
                        }else return false;
                    }
                    if(ss[ret]=='('){
                        if(stack.peek()==')'){
                            stack.pop();
                        }else return false;
                    }
                }
                ret++;
            }
            if(!stack.isEmpty())return false;
            return true;
        }
    }
}
